package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.EsaaT;
import cn.e4j.modular.crm.mapper.EsaaTMapper;
import cn.e4j.modular.crm.service.EsaaTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuw
 * @since 2019-11-06
 */
@Service
public class EsaaTServiceImpl extends ServiceImpl<EsaaTMapper, EsaaT> implements EsaaTService {

    @Override
    public List<Map<String, Object>> getEsaalist(Page<Object> page, Dto inDto) {
        return this.baseMapper.getEsaalist(page, inDto);
    }

    @Override
    public boolean existedTheParams(EsaaT esaaT) {
        QueryWrapper<EsaaT> gwfaTQueryWrapper = new QueryWrapper<>();
        gwfaTQueryWrapper.eq("ESAADOCNO", esaaT.getESAADOCNO());
        return this.baseMapper.selectList(gwfaTQueryWrapper).size() > 0;
    }

    @Override
    @Transactional
    public void add(EsaaT esaaT) {
        esaaT.setESAADOCNO(DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        esaaT.setESAASTUS("1");
        esaaT.insert();
    }

    @Override
    @Transactional
    public void edit(EsaaT esaaT) {
        UpdateWrapper<EsaaT> gwfaTQueryWrapper = new UpdateWrapper<>();
        gwfaTQueryWrapper.eq("ESAADOCNO", esaaT.getESAADOCNO());
        esaaT.update(gwfaTQueryWrapper);
    }

    @Override
    @Transactional
    public void delete(EsaaT esaaT) {
        UpdateWrapper<EsaaT> gwfaTQueryWrapper = new UpdateWrapper<>();
        gwfaTQueryWrapper.eq("ESAADOCNO", esaaT.getESAADOCNO());
        esaaT.delete(gwfaTQueryWrapper);
    }

    @Override
    @Transactional
    public void deleteBatch(List<EsaaT> esaaTList) {
        List<String> idList = new ArrayList<>();
        for (EsaaT esaaT : esaaTList) {
            idList.add(esaaT.getESAADOCNO());
        }
        this.removeByIds(idList);
    }

    @Override
    @Transactional
    public void changeStatusBatch(List<EsaaT> esaaTList) {
        EsaaT updateEsaaT = new EsaaT();
        updateEsaaT.setESAASTUS(esaaTList.get(0).getESAASTUS());
        for (EsaaT esaaT : esaaTList) {
            UpdateWrapper<EsaaT> gwfaTQueryWrapper = new UpdateWrapper<>();
            gwfaTQueryWrapper.eq("ESAADOCNO", esaaT.getESAADOCNO());
            updateEsaaT.update(gwfaTQueryWrapper);
        }
    }

    @Override
    public void transferBatch(List<EsaaT> esaaTList) {
        EsaaT updateEsaaT = new EsaaT();
        updateEsaaT.setESAA010(esaaTList.get(0).getESAA010());
        for (EsaaT esaaT : esaaTList) {
            UpdateWrapper<EsaaT> gwfaTQueryWrapper = new UpdateWrapper<>();
            gwfaTQueryWrapper.eq("ESAADOCNO", esaaT.getESAADOCNO());
            updateEsaaT.update(gwfaTQueryWrapper);
        }
    }
}
